import React from 'react';
import {
  View,
  ScrollView,
  TouchableOpacity,
  Platform,
  Alert,
} from 'react-native';
import {Text, Button, ListItem} from '@rneui/base';
import {
  useMap,
  useMount,
  useSafeState,
  useSetState,
  useUpdateEffect,
} from 'ahooks';
import {round} from 'lodash';
import style from './style.less';
import {useNavigation} from '@react-navigation/native';
import {FileSaveDirName} from '@/config/common';
import dayjs from 'dayjs';
import CheckComPowers from '@/components/CheckComPowers';
import ImagePicker from 'react-native-image-crop-picker';
import {Dirs, FileSystem} from 'react-native-file-access';
import RNFS from 'react-native-fs';
import * as RNPath from 'react-native-path';

export {default as config} from './config';
export default props => {
  const navigation = useNavigation();
  return (
    <ScrollView style={style.box}>
    
      <CheckComPowers clickPowers={['CAMERA', 'WRITE_EXTERNAL_STORAGE']}>
        <Button
          title="打开录像"
          containerStyle={style.marginTop10}
          onPress={async () => {
            try {
              let tmpFileInfo = await ImagePicker.openCamera({
                mediaType: 'video',
              });
              let parseInfo = RNPath.parse(tmpFileInfo.path);
              let newDir = `${Dirs.SDCardDir}/${FileSaveDirName}/video`;
              let newFilename = `${dayjs().format('YYYYMMDDHHmmssSSS')}${
                parseInfo.ext
              }`;
              await RNFS.mkdir(newDir);
              let newPath = `${newDir}/${newFilename}`;
              let binary = await RNFS.readFile(tmpFileInfo.path, 'base64');
              await RNFS.writeFile(newPath, binary, 'base64');
              console.log('newPath', newPath, binary.length);
            } catch (e) {
              console.log('异常', e.message);
            }
          }}
        />
      </CheckComPowers>
    </ScrollView>
  );
};
